<?php

class NORestApi {
    private $db;
    private $queue;

    public function __construct($db) {
        $this->db = $db;
        $this->queue = new Queue($this->db);
    }

    public function handleRequest() {
        switch ($_GET['message']) {
            case 'meterReading':
                if ($this->isValidMessageParameters()) {
                    return $this->addMeterReadingJobs();
                }
                return 'Wrong parameters';
            break;
            default:
                return 'Unknown message';
            break;
        }
    }

    private function isValidMessageParameters() {
        $clientValidator = new Validator_Client();
        $client = new Client_NO2_Client($_GET['clientId'], '');
        if (!$clientValidator->isValid($client)) {
            return false;
        }
        $esValidator = new Validator_ES();
        $es = new Client_NO2_ES($_GET['esId'], '');
        if (!$esValidator->isValid($es)) {
            return false;
        }
        if (!floatval($_GET['reading'])) {
            return false;
        }
        return true;
    }

    private function addMeterReadingJobs() {
        $clientData = explode('.', $_GET['clientId']);
        // Creating jobs for sending meter readings to RK1, RK2, ES1 and ES2
        // ES1
        $NOId = $clientData[0];
        $areaId = $clientData[1];
        $facilityId = $clientData[2];
        $meterValue = round($_GET['reading']);
        $date = date('Y-m-d');
        $meteringMessage = new Client_ES1_meteringMessage($areaId, $date, $facilityId, $meterValue, $NOId);
        $sendMeteringReadings = new Client_ES1_SendMeteringReadings($meteringMessage);
        $this->queue->add('Client_ES1', 'sendMeterReading', $sendMeteringReadings);

        // ES2
        $messageId = '';
        $messageRecipient = 'T2ES2';
        $sendTime = date('Y-m-d');
        $messageSender = $NOId;
        $relatedMessageId = '';
        $readingValue = $_GET['reading'];
        $estimated = false;
        $readingStartDate = date('Y-m-d');
        $readingEndDate = date('Y-m-d');

        $id = $facilityId;
        $name = '---';
        $postalAddress = '';
        $startDate = date('Y-m-d');
        $endDate = date('Y-m-d');
        $phone = '';
        $client = new Client_ES2_client($id, $name, $postalAddress, $startDate, $endDate, $phone);

        //$client = $_GET['clientId'];
        $meterData = new Client_ES2_meterData($messageId, $messageRecipient, $sendTime, $messageSender, $relatedMessageId, $readingValue, $estimated, $readingStartDate, $readingEndDate, $client);
        $submitMeterData = new Client_ES2_submitMeterData($meterData);
        $this->queue->add('Client_ES2', 'sendMeterReading', $submitMeterData);

        // RK1
        $networkOwnerId = $NOId;
        $constraintAreaId = $areaId;
        $startDate = date('Y-m-d H:i:s');
        $endDate = date('Y-m-d H:i:s');
        $electricityUserId = $_GET['clientId'];
        $consumedEnergy = $_GET['reading'];
        $electricitySupplierId = $_GET['esId'];
        $suppliedEnergy = $_GET['reading'];
        $meteredData = new Client_RK1_receiveDailyMeteredData($networkOwnerId, $constraintAreaId, $startDate, $endDate, $electricityUserId, $consumedEnergy, $electricitySupplierId, $suppliedEnergy);
        $this->queue->add('Client_RK1', 'sendMeterReading', $meteredData);

        // RK2
        $startDate = date('Y-m-d H:i:s');
        $endDate = date('Y-m-d H:i:s');
        $entryPoint = $_GET['reading'];
        $exitPoint = $_GET['reading'];
        $networkOwnerID = $NOId;
        $electricitySupplierID = $_GET['esId'];
        $userID = $_GET['clientId'];
        $constraintAreaID = $_GET['esId'];
        $meteredData = new Client_RK2_saveMeteredData($startDate, $endDate, $entryPoint, $exitPoint, $networkOwnerID, $electricitySupplierID, $userID, $constraintAreaID);
        $this->queue->add('Client_RK2', 'sendMeterReading', $meteredData);

        $str = 'Client Id: ' . $_GET['clientId'];
        $str .= "; ";
        $str .= 'ES Id: ' . $_GET['esId'];
        $str .= "; ";
        $str .= 'Reading: ' . $_GET['reading'];
        Logger::log('REST server', $str);
        return true;
    }
}
